﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;

namespace BLL
{
  public class VideoBLL
  {
    public List<Video> GetAll()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Videos.ToList();
      }
    }
    public List<Video> GetAll(bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Videos.Where(p => p.isEnable == isEnable).ToList();
      }
    }
    public Video GetById(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        return data.Videos.Where(p => p.Id == id).FirstOrDefault();
      }
    }

    public string GetLink()
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
       Video video = data.Videos.Where(p => p.isEnable == true).FirstOrDefault();
       if (video != null)
         return video.Link;
       else return "";
      }
    }

    public Video GetFirst()
    {
        using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
        {
            return data.Videos.Where(p => p.isEnable == true).FirstOrDefault();
        }
    }

    public void Enable(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Video obj = data.Videos.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = !obj.isEnable;
        data.SubmitChanges();
      }
    }

    public void Enable(int id, bool isEnable)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Video obj = data.Videos.Where(p => p.Id == id).FirstOrDefault();
        obj.isEnable = isEnable;
        data.SubmitChanges();
      }
    }

    public void Update(Video obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Video video = data.Videos.Where(p => p.Id == obj.Id).FirstOrDefault();
        if (video != null)
        {
          video.isEnable = obj.isEnable;
          video.Link = obj.Link;
          video.Name = obj.Name;
          video.Position = obj.Position;
          video.isYoutube = obj.isYoutube;
          data.SubmitChanges();
        }
      }
    }

    public void Add(Video obj)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        data.Videos.InsertOnSubmit(obj);
        data.SubmitChanges();
      }
    }

    public void Delete(int id)
    {
      using (var data = new DigitalColorCMSDataContext(_Global.StringConnect))
      {
        Video video = data.Videos.Where(p => p.Id == id).FirstOrDefault();
        if (video != null)
        {
          data.Videos.DeleteOnSubmit(video);
          data.SubmitChanges();
        }
      }
    }
  }
}
